<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"  lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8" />
    <title>组织管理</title>
    <script src="/plugin/jquery/jquery-1.9.0.min.js" type="text/javascript"></script>
    <link href="/plugin/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
    <script src="/plugin/ligerUI/js/ligerui.all.js" type="text/javascript"></script>
    <link href="/plugin/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
    <link href="/plugin/ligerUI/skins/Gray/css/all.css" rel="stylesheet" type="text/css" />
    <script src="/plugin/ligerUI/js/core/init-obj.js" type="text/javascript"></script>

    <link href="/css/form.css" rel="stylesheet"/>

    <script src="/plugin/jquery/jquery.validate.js"></script>
    <script src="/js/mask.js" type="text/javascript"></script>

    <style type="text/css">
        .tools{width: 95%;height: 25px; margin: 10px auto;}
        .users{display: none;}
        /* 半透明的遮罩层 */
        #overlay {
            background: #000;
            filter: alpha(opacity=50); /* IE的透明度 */
            opacity: 0.5;  /* 透明度 */
            display: none;
            position: absolute;
            top: 0px;
            left: 0px;
            width: 100%;
            height: 100%;
            z-index: 50; /* 此处的图层要大于页面 */
            display:none;
        }
        #dig{display: none;}
    </style>
    <script th:inline="javascript">
        /*<![CDATA[*/
        /*]]>*/
        var navtab = null;
        var tree8 =null;
        $(function ()
        {
            $("#layout1").ligerLayout({ leftWidth: 150});
            $("#tab1").ligerTab();
            navtab = $("#tab1").ligerGetTabManager();

            doSearch();
            $(".users").show();
            tree8= $("#tree8").ligerTree({
                url:'/sec/org/tree',
                ajaxType:'post',
                checkbox: false,
                parentIcon: 'folder',
                childIcon: 'leaf' ,
                slide: false, //是否显示动画
                nodeWidth: 100,
                idFieldName: 'id',
                textFieldName:'name' ,
                onSelect: function (node)
                {

                    $("#PID").val(node.data.id);
                    $("#PNAME").val(node.data.name);
                    doSearch();
                    $(".users").show();
                    $.ajax({
                        type: 'POST',
                        url: 'detail',
                        data: {
                            id:node.data.id
                        },
                        dataType: 'json',
                        success: function(data){
                            $("#id").val(data.data.id);
                            $("#deleted").val(data.data.deleted);
                            $("#creator").val(data.data.creator);
                            $("#parentId").val(data.data.parentId);

                            var date = new Date(parseInt(data.data.createTime,
                                10));
                            $("#createTime").val(date);

                            $("#orgCode").val(data.data.orgCode);
                            $("#orgName").val(data.data.orgName);
                        },
                        error:function(){
                            $.ligerDialog.error('加载数据失败');
                        }
                    });

                },
                onError:function(node){
                    $.ligerDialog.error('组织列表加载失败!');
                }
            });

        });

        function add(){
            if($("#PID").val()!='') {
                showOverlay();
                var dlgedit = $.ligerDialog.open({
                    title: "添加组织",
                    target: $("#dig"),
                    width: 400,
                    height: 100,
                    buttons: [{
                        text: '保存', onclick: function (i, d) {
                            if ($("#PID").val() != '') {
                                save();
                            }
                            else {
                                $.ligerDialog.error('请选择一个组织节点');
                            }
                        }
                    },
                        {
                            text: '关闭', onclick: function (i, d) {
                            $("input").ligerHideTip();
                            d.hide();
                            hideOverlay();
                            menus.reload();
                        }
                        }
                    ]
                });
                $(".l-dialog-close").bind('mousedown', function ()  //dialog右上角的叉
                {
                    $("input").ligerHideTip();
                    dlgedit.hide();
                    hideOverlay();
                });
            }

            else{
                $.ligerDialog.error('请选择一个组织节点');
            }

        }

        function save() {
            var obj = new Object();
            obj.orgName=$("#orgName1").val();
            obj.deleted=0
            obj.createTime=new Date();
            obj.parentId=$("#PID").val();

            $.ajax({
                type: 'POST',
                url: 'add',
                data: obj,
                dataType: 'json',
                success: function(data){
                    if(data.code==200){
                        $.ligerDialog.success('保存成功');
                        tree8.reload();
                    }else{
                        $.ligerDialog.error('保存失败');
                    }
                },
                error:function(){
                    $.ligerDialog.error('保存失败');
                }
            });
        }

        function deleted(){
            var id = $("#PID").val();
            var name = $("#PNAME").val();
            if(id!=''){
                deletedRows(id,name);
            }else{
                $.ligerDialog.error("请选择要删除的组织");
            }
        }
        var dataGrid=null;
        function loadDataAptitude(data){
            dataGrid   =$("#maingrid").ligerGrid({
                checkbox:true,
                height: '100%',
                columns: [
//                    { display: '操作', value: '操作', minWidth: 120,render: function (rowdata)
//                    {
//                        return '<a onclick=menuTree("'+rowdata.id+'") href=javasrcipt:;>权限</a>';
//                    }
//                    },
                    { display: '用户名', name: 'userName', minWidth: 120 },
                    { display: '性别', name: 'sex' ,minWidth: 140,render: function (rowdata)
                    {
                        if (rowdata.sex == 0)
                        {
                            return "女";
                        }
                        return "男";
                    }
                    },
                    { display: '账号', name: 'account',minWidth: 180 },
                    { display: '手机号码', name: 'mobile', minWidth: 140 },
                    { display: '邮箱', name: 'email' ,minWidth: 140},
                    { display: '地址', name: 'addr' ,minWidth: 140},
                    { display: '状态', name: 'status' ,minWidth: 140,render: function (rowdata)
                    {
                        if (rowdata.status == 0)
                        {
                            return "不可用";
                        }
                        return "可用";
                    }},
                    { display: '创建人', name: 'creator' ,minWidth: 140},

                    { display: '创建时间', name: 'createTime', minWidth: 140 ,type:'date'}
                ],
                parms:data,
                rownumbers: true,
                autoFilter: true,
                dataAction:"local",
                url:"/sec/user/list",
                usePager:true,
                pageSize:"20",//分页页面大小
                pageSizeOptions:[10,20,30]//可指定每页页面大小
            });
            var s=  $(".l-panel-topbarinner .l-toolbar-item:last").find(".l-icon-search2");
            if(s.length==1){
                $(".l-panel-topbarinner .l-toolbar-item:last").remove();
            }
        }

        function doSearch(){
            var data = $("#searchFrom").serializeArray();
            loadDataAptitude(data);
        }

        function doReset(){
            $("#searchFrom")[0].reset();
        }

        function sub(){



            if($("#PID").val()!='') {

                var path = null;

                if ($("#id").val() != '') {
                    path = 'update';
                } else {

                    path = 'add';
                    $("#deleted").val('0');
                    $("#creator").val('');
                    $("#parentId").val($("#PID").val());

                    $("#createTime").val(new Date());
                }
                if($("#orgName").val()!=''&&$("#orgCode").val()!=''){
                    send(path);
                }else{
                    $.ligerDialog.error('不能为空');
                }


            }
            else{
                $.ligerDialog.error('请选择一个组织节点');
            }
        }

        function send(path){
            var data = $("#subForm").serialize();
            $.ajax({
                type: 'POST',
                url: path,
                data: data,
                dataType: 'json',
                success: function (data) {
                    if (data.code == 200) {
                        $.ligerDialog.success("保存成功");
                        tree8.reload();
                    } else {
                        $.ligerDialog.error('保存失败');
                    }
                },
                error: function () {
                    $.ligerDialog.error('保存失败');
                }
            });

        }

        function f_selectNode(str) {
            str = "," + str + ",";
            var parm = function(data) {
                if (str.indexOf("," + data.id + ",") != -1)
                { return true; } else { return false }
            };
            tree8.selectNode(parm);
        };

        function deletedRows(ids,name){
            $.ligerDialog.confirm('确定删除'+name+"吗？", function (yes){
                if(yes) {
                    $.ajax({
                        type: 'POST',
                        url: 'deleteBacth',
                        data: {
                            ids: ids
                        },
                        dataType: 'json',
                        success: function (data) {
                            if(data.code==200){
                                location.reload()
                            }else{
                                $.ligerDialog.error('删除失败');
                            }
                        },
                        error:function(){
                            $.ligerDialog.error('删除失败');
                        }
                    });
                }
            });
        }

        var paths = '';
        function getChildName(node){
            console.log(node)
            if(node.data==undefined){
                paths+=node.name+",";
                if(node.parentId!='0'){
                    var lev = node.treedataindex;
                    var pnode =tree8.getParent(node, lev-1);
                    getChildName(pnode);
                }
            }
            else{
                paths+=node.data.name+",";
                if(node.data.parentId!='0'){
                    var lev = node.data.treedataindex;
                    var pnode =tree8.getParent(node, lev-1);
                    getChildName(pnode);
                }
            }
        }



        function itemclick(item)
        {
            var height = $(".l-layout-center").height();
            if(item=='update'){

                var obj = dataGrid.getSelected();
                if(obj!=null)
                    f_addTab1(item.id,'修改用户','linkUpdate?id='+obj.id,navtab,height);
                else
                    $.ligerDialog.question('请选择一行数据后编辑');
            }
            else if(item=='add'){
                if($("#PID").val()!='')
                    f_addTab1(item,'添加用户','/sec/org/orgLoad',navtab,height);

                else
                    $.ligerDialog.question('请选择组织节点');
            }
            else {
                var obj= dataGrid.getSelecteds();
                if(obj.length>0){
                    var ids = '';
                    /*<![CDATA[*/
                    for (var i = 0; i < obj.length; i++) {
                        ids += obj[i].id + ",";
                    }
                    $.ajax({
                        type: 'POST',
                        url: '/sec/user/userAddorg',
                        data: {
                            ids:ids,
                            orgId:'0'
                        },
                        dataType: 'json',
                        success: function(data){
                            if(data.code==200){
                                $.ligerDialog.success('删除成功');
                                dataGrid.reload();
                            }else{
                                $.ligerDialog.error('保存失败');
                            }
                        },
                        error:function(){
                            $.ligerDialog.error('保存失败');
                        }
                    });
                }
                else{
                    $.ligerDialog.question('请选择要删除的数据');
                }
            }
        }

        function callBack()
        {
            navtab.removeTabItem('add');
            navtab.removeTabItem('update');
            dataGrid.reload();
            $.ligerDialog.success('保存成功');
        }
        var tabs =null
        //子页面回调函数
        function cellValOrgId(){
            return $("#PID").val();
        }
    </script>
</head>
<body style="padding:4px">
<div id="layout1">
    <div  position="left" title="组织树">
        <ul id="tree8"></ul>
    </div>
    <div id="plans"  position="center" title="组织详情信息">
        <input type="hidden"  id="orgIdF" value=""/>
        <div class="tools">
                <a  shiro:hasPermission="/sec/org/linkAdd"      onclick="add()" >添加</a>
                <a  shiro:hasPermission="/sec/org/delete"       onclick="deleted()"  >删除</a>
                <a  shiro:hasPermission="/sec/org/linkUpdate"   onclick="sub()" >保存数据</a>
        </div>

        <div style="margin:auto;">
            <form id="searchFrom">
                <input type="hidden"  id="PID" name="orgId" value="" />
            </form>
            <input type="hidden"  id="PNAME" value="" />

         <form id="subForm">
             <div class="formDiv" style="width: 95%;">
                 <input type="hidden" name="id" id="id">
                 <input  type="hidden" name="deleted" id="deleted">
                 <input  type="hidden" name="parentId" id="parentId">
                 <input  type="hidden" name="creator" id="creator">
                 <input  type="hidden" name="createTime" id="createTime">
                 <div class="formLine">
                     <div class="formInp">
                         <span>组织名称：</span>
                         <input placeholder="暂未选中任何组织"  class="blue testTxt" type="text" id="orgName" name="orgName" value=""/>
                     </div>
                 </div>
             </div>
         </form>
     </div>

        <div style="width: 95%; margin: auto; " class="users">
            <div id="tab1" style="width: 100%;overflow:hidden;">
                <div id="tis" title="用户列表">
                    <div>
                        <a shiro:hasPermission="/sec/user/orgLoad"  href="javascript:itemclick('add')">增加用户</a>
                        <a shiro:hasPermission="/sec/user/orgDelete"  href="javascript:itemclick('delete')">删除用户</a>
                    </div>
                    <div class="l-clear"></div>
                    <div  id="maingrid"></div>
                </div>
            </div>
        </div>
    </div>
     </div>

</div>
<div id="dig">
    <div class="formInp">
        <span>组织名称：</span>
        <input  class="blue testTxt" type="text" id="orgName1"  value=""/>
    </div>
</div>

<div id="overlay"></div>

</body>
</html>